layui.define(['element', 'api', 'utils'], function(exports) {
	"use strict";

	/**
	 * Button component
	 * */
	var MOD_NAME = 'ztree',
		$ = layui.jquery,
		api = layui.api,
        utils = layui.utils,
		elem,
		treeOption
    ;

	var ztree = function(opt) {
		this.option = opt;
	};


	function resetContent(){
		var $parent = $(elem).parent();
		$(elem).remove();
		$parent.append('<ul class="ztree" id="'+elem+'"></ul>')
	}

	ztree.prototype.refresh = function (){
		//当前树 id
		elem = utils.contextData.currentTreeId;
		resetContent();
	}

    /**
	 * Button start loading
	 * */
	 ztree.prototype.render = function(opt) {
		
		var option = {
			elem: opt.elem,
			enableRootPath:opt.enableRootPath,
			api: opt.api,
            setting: opt.setting,
            // zNodes: opt.zNodes,
			tree:null
		}

		 treeOption = option;
		 elem = option.elem;
         api.get({
             enableRootPath: option.enableRootPath,
             url: option.api,
             async: false,
             success: function(data){
                 option.tree = $.fn.zTree.init($(option.elem), option.setting, data);
             }
         });

        return new ztree(option);
	}


    exports(MOD_NAME, new ztree());
});
